package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.ApArticleHtmlTask;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import javassist.runtime.DotClass;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.krb5.Config;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-02-13
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    private IApArticleContentService apArticleContentService;

    @Autowired
    private ApArticleHtmlTask apArticleHtmlTask;
    @Override
    public ResponseResult<Long> saveApArticle(ArticleDto dto) {
//        保存两个表的数据
        //        ApArticle;
//        ApArticleContent
//        先判断是否有文章id
        if (dto.getId() == null) { //新增
            ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);
            apArticle.setCreatedTime(new Date());
            this.save(apArticle);

            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);

            apArticleHtmlTask.createHtml(apArticle,dto.getContent());


            return ResponseResult.okResult(apArticle.getId());

        }else{
            ApArticle apArticle = new ApArticle();
            apArticle.setCreatedTime(new Date());
            BeanUtils.copyProperties(dto,apArticle);
            this.updateById(apArticle);
//            先根据文章id查询修改内容再保存

            LambdaQueryWrapper<ApArticleContent> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApArticleContent::getArticleId,dto.getId());
            ApArticleContent one = apArticleContentService.getOne(queryWrapper);
            one.setContent(dto.getContent());
            apArticleContentService.updateById(one);

            apArticleHtmlTask.createHtml(apArticle,dto.getContent());

            return ResponseResult.okResult(apArticle.getId());

        }
    }

    @Override
    public ResponseResult<Long> load(ArticleHomeDto dto, int type) {
//        type:0  文章的发布时间>maxTime   type:1 文章的发布时间<minTime


        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
//        1、频道ID==0 查询的所有的频道
        if(dto.getChannelId()!=0){
            queryWrapper.eq(ApArticle::getChannelId,dto.getChannelId());
        }
//        2、根据文章的发布时间
        if(type==0){
            queryWrapper.gt(ApArticle::getPublishTime,dto.getMaxTime());
        }else{
            queryWrapper.lt(ApArticle::getPublishTime,dto.getMinTime());
        }

//        两个隐藏的条件 没有下架 没有删除
        queryWrapper.eq(ApArticle::getIsDelete,false);
        queryWrapper.eq(ApArticle::getIsDown,false);


//        3、文章的发布时间排序
        queryWrapper.orderByDesc(ApArticle::getPublishTime);

//        分页
        IPage<ApArticle> page = new Page<>(1, dto.getSize());
        IPage<ApArticle> page1 = this.page(page, queryWrapper);

        return ResponseResult.okResult(page1.getRecords());
    }

    @Override
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {

        ApArticle apArticle = this.getById(dto.getArticleId());
        if(apArticle==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<ApArticleContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleContent::getArticleId,dto.getArticleId());
        ApArticleContent articleContent = apArticleContentService.getOne(queryWrapper);

//         config:ApArticle对象
//         content:ApArticleContent对象

        Map map = new HashMap();
        map.put("config",apArticle);
        map.put("content",articleContent);
        return ResponseResult.okResult(map);
    }
}
